                                # 7.2 while循环简介
# 7.2.1 使用while循环
    # while 循环不断地运行，直到指定的条件不满足为止
current_number = 1
while current_number <= 5:
    print(current_number)
    current_number += 1
# 7.2.2 让用户选择何时退出
prompt = "\nTell me something, and I will repeat it back to you:"
prompt += "\nEnter 'quit' to end the program."
message = "default"
# 首次执行语句时需要将message和'quit'比较，但此时用户还没有输入
# 如果没有可供比较的东西，将无法继续运行程序。
# 为解决这个问题，我们必须给变量message一个初始值。可以是空串
while message != "quit":
    message = input(prompt)
    if message != 'quit':
        print(message)
# 7.2.3 使用标志
prompt = "\nTell me something, and I will repeat it back to you:"
prompt += "\nEnter 'quit' to end the program."
message = "default"

active = True # 添加一个标志，判断程序是否应该继续运行
while active:
    message = input(prompt)
    if message == 'quit':
        active = False
    else:
        print(message)
# 7.2.4 使用break退出循环
prompt = "\nPlease enter the name of a city you have visited:"
prompt += "\n(Enter 'quit' when you are finished.)"

while True:
    city = input(prompt)

    if city == 'quit':
        break
    else:
        print("I'd love to go to " + city.title() + "!")
# 7.2.5 在循环中使用continue
    # 从1数到10，但只打印其中奇数的循环
current_number = 0
while current_number < 10:
    current_number += 1
    if current_number % 2 == 0:
        continue # 若为偶数，执行continue语句，忽略以下的代码，返回循环开头

    print(current_number)
# 7.2.6 避免无限循环
                            # 7.3 使用while循环来处理列表和字典
# 7.3.1 在列表之间移动元素
# 首先，创建一个待验证用户列表
# 和一个用于存储已验证用户的空列表
unconfirmed_users = ['alice', 'brain', 'candace']
confirmed_users = []
# 验证每个用户，直到没有未验证用户为止
# 将每个经过验证的列表都移到已验证用户列表中
while unconfirmed_users: # 循环将不断进行，直到列表变成空的
    current_user = unconfirmed_users.pop() # 以每次一个的方式从列表末尾删除并存储到新变量中

    print("Verifying users: " + current_user.title())
    confirmed_users.append(current_user)
# 显示所有已验证用户
print("\nThe following users have been confirmed:")
for confirmed_user in confirmed_users:
    print(confirmed_user.title())
# 7.3.2 删除包含特定值的所有列表元素
pets = ['dog', 'cat', 'dog', 'goldfish', 'cat', 'rabbit', 'cat']
print(pets)

while 'cat' in pets:
    pets.remove('cat')
    print("one cat removed")

print(pets)
# 7.3.3 使用用户输入来填充字典
responses = {}

# 设置一个标志，指出调查是否继续
polling_active = True

while polling_active:
    # 提示输入被调查者的名字和回答
    name = input("\nWhat is your name? ")
    response = input("\nWhich mountain would you like to climb someday? ")
    # 将答卷存储在字典中
    responses[name] = response
    # 看看是否还有人要参与调查
    repeat = input("Would you like to let another person respond? (yes/ no) ")
    if repeat == 'no':
        polling_active = False
# 调查结束，显示结果
print("--- Poll Results ---")
for name, response in responses.items():
    print(name + " would like to climb " + response + ".")